<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        .box {

            width: 100px;
            height: 100px;
            position: absolute;
            left: 0px;
            top: 0px;
            background: red;
        }
    </style>
</head>

<body>
    <div class="box"></div>
    <script>
        // 二 多方向运动；
        function move(ele, target, dir, cb) {
            var timer = setInterval(function () {
                // 获取 方块的left值；
                var num = parseInt(getComputedStyle(ele)[dir]);
                var speed = target < num ? -10 : 10;
                // console.log(num);
                if (num == target) {
                    console.log("停止了");
                    clearTimeout(timer);

                    cb && cb();

                } else {
                    num += speed;
                    ele.style[dir] = num + "px";
                }
            }, 100);
        }
        var boxEle = document.querySelector(".box");
        // 回调地狱；：回调函数出现层层嵌套；
        move(boxEle, 300, "left", function () {
            console.log("右运动完成")

            move(boxEle, 300, "top", function () {
                console.log("向下运动完成");
                move(boxEle, 0, "left", function () {
                    move(boxEle, 0, "top", function () {
                        console.log("所有运动完成");
                    })
                });
            });
        });  //右边

    </script>
</body>

</html>